import {Component} from '@angular/core';
import {PageVoService} from '../../service/page-vo.service';
import {ActivatedRoute, Router} from '@angular/router';
import {DocService} from '../../service/doc.service';
import {MessageService} from '../../service/message.service';
import {BaseDocComponent} from '../../doc/page-doc/base-doc.component';
import {InvokeLogService} from '../../service/invoke-log.service';
import {InvokeLogQuery} from '../../entity/invoke-log-query';
import {PageVo} from '../../entity/page-vo';
import {LinkDto} from '../../entity/link-dto';
import {PagedList} from '../../entity/paged-list';
import {InvokeLog} from '../../entity/invoke-log';
import {AlertMessage} from '../../entity/alert-message';

@Component({
    selector: 'app-invoke-log',
    templateUrl: './invoke-log.component.html',
    styleUrls: ['./invoke-log.component.css']
})
export class InvokeLogComponent extends BaseDocComponent {
    query: InvokeLogQuery;

    data: PagedList<InvokeLog>;

    constructor(protected pageVoService: PageVoService,
                protected activatedRoute: ActivatedRoute,
                protected docService: DocService,
                protected router: Router,
                protected messageService: MessageService,
                private invokeLogService: InvokeLogService) {
        super(activatedRoute, router, pageVoService, docService, messageService);
    }

    onDocReady() {
        this.query = <InvokeLogQuery>{
            docId: this.docId,
            apiKey: this.activatedRoute.snapshot.queryParamMap.get('apiKey'),
            userId: this.getNumberParam('userId'),
            type: this.getNumberParam('type'),
            envCode: this.activatedRoute.snapshot.queryParamMap.get('envCode'),
            responseStatus: this.getNumberParam('status'),
            pageNo: this.getNumberParam('pageNo'),
            pageSize: this.getNumberParam('pageSize'),
        };
        if (!this.query.pageSize) {
            this.query.pageSize = 20;
        }
        if (!this.query.pageNo) {
            this.query.pageNo = 1;
        }
        this.invokeLogService.list(this.query).subscribe(resp => {
            if (resp.code === 200) {
                this.data = resp.data;
            } else {
                this.messageService.add(<AlertMessage>{
                    type: 'error',
                    html: '加载接口调用日志失败：' + resp.message,
                    time: 8
                });
            }
        });
    }

    protected getActiveMenuId() {
        return 'invoke-log';
    }

    setPageVo(basePageVo: PageVo) {
        basePageVo.crumbs.push(<LinkDto> {
            text: '调用日志',
            frontIcon: 'date_range'
        });
    }

    queryLog(name: string, value: any) {
        this.query[name] = value;
        this.loadInvokeLogs();
    }

    loadPage(pageNo) {
        this.query.pageNo = pageNo;
        this.loadInvokeLogs();
    }

    private getNumberParam(name: string) {
        const strVal = this.activatedRoute.snapshot.queryParamMap.get(name);
        if (!strVal) {
            return null;
        }
        return Number.parseInt(strVal, 10);
    }

    private loadInvokeLogs() {
        this.router.navigate(['doc', this.docId, 'invoke-log'], {
            queryParams: {
                userId: this.query.userId,
                apiKey: this.query.apiKey,
                type: this.query.type,
                envCode: this.query.envCode,
                responseStatus: this.query.responseStatus,
                pageNo: this.query.pageNo === 1 ? null : this.query.pageNo,
                pageSize: this.query.pageSize === 20 ? null : this.query.pageSize,
            }
        });
    }
}
